<template>
  <div class="page-content">
    <div v-wechat-title="$route.meta.title"></div>
    <mt-field label="单位名称"
              class="must"
              placeholder="请输入单位名称"
              v-model="company"></mt-field>
    <mt-field label="单位地址"
              class="must"
              placeholder="请输入单位地址"
              v-model="address"></mt-field>
    <mt-field label="申请人姓名"
              class="must"
              placeholder="请输入申请人姓名"
              v-model="name"></mt-field>
    <mt-field label="证件类型"
              class="must"
              disabled>
      <mt-popup v-model="popupVisible"
                popup-transition="bottom"
                closeOnClickModal="true"
                position="bottom">
        <mt-picker :slots="ID_type"
                   @change="onshipChange"
                   showToolbar>
          <mt-button type="primary"
                     class="usi-btn-sure"
                     @click="popupVisible = !popupVisible"
                     size="small">确定</mt-button>
        </mt-picker>
      </mt-popup>
      <div class="my-content-list row justfy"
           @click="ShouPup">
        <input readonly
               v-model="idType"
               placeholder="请选择" />
        <img class="arrow"
             src="../assets/images/arrow-left.png"
             alt />
      </div>
    </mt-field>
    <mt-field label="证件号码"
              class="must"
              placeholder="证件号码"
              v-model="idcard"
              @blur.native.capture="checkIDcard()"></mt-field>
    <mt-field label="申请人手机"
              placeholder="请输入申请人手机"
              class="must"
              type="tel"
              v-model="phone"
              @blur.native.capture="checkPhone()"></mt-field>
    <mt-button type="primary"
               class="addcard"
               size="small"
               @click="addHealthCard">提交</mt-button>
  </div>
</template>
<script>
import "common/css/form.styl";
import wechat from '@/assets/js/wechat.js';// 跳转微信 获取权限
export default {
  data () {
    return {
      name: "测试名称", //申请人姓名
      company: "万达信息", //公司名称
      address: "联航路", //公司地址
      phone: "17839949351", //手机号
      idcard: "410327199408021416", //身份证号
      idType: "",
      idTypeCode: "",
      popupVisible: false,
      RSA: "",
      ID_type: [
        //证件类型
        {
          values: ["身份证", "护照", "港澳台通行证"]
        }
      ]
    };
  },
  watch: {},
  methods: {
    checkIDcard () {
      if (this.idcard) {
        if (!this.$public.isIDcard(this.idcard)) {
          this.$public.toast("身份证格式有误！");
        }
      }
    },
    checkPhone () {
      if (this.phone) {
        if (!this.$public.isPhone(this.phone)) {
          this.$public.toast("手机号码格式有误！");
        }
      }
    },
    onshipChange (picker, values) {
      this.idType = values;
      if (values[0] > values[1]) {
        picker.setSlotValue(1, values[0]);
      }
    },
    ShouPup () {
      this.popupVisible = true;
    },
    addHealthCard () {
      let _that = this;
      $.pubVariable.idCardType.forEach((item, i) => {
        if (item.text == this.idType[0]) {
          this.idTypeCode = item.id;
        }
      });
      if (!this.name || !this.company || !this.address || !this.idTypeCode) {
        this.$public.toast("信息不完整！");
      } else if (!this.$public.isIDcard(this.idcard)) {
        this.$public.toast("身份证格式有误！");
      } else if (!this.$public.isPhone(this.phone)) {
        this.$public.toast("手机号码格式有误！");
      } else {
        // 校验成功
        wechat.setRSA((RSA) => {
          this.$axios.http
            .post({
              funcName: "/fwdj/company/register",
              params: {
                dwmc: this.company,
                dwdz: this.address,
                sqrxm: RSA(encodeURI(this.name), true),
                sqrzjlx: this.idTypeCode,
                sqrzjhm: RSA(this.idcard, true),
                sqrsjh: RSA(this.phone, true),
                id: sessionStorage.getItem("openId")
              }
            })
            .then(res => {

              if (res && res.code == "200") {
                sessionStorage.setItem("companyInfo", JSON.stringify(res.company));
                this.$router.push({ name: "register-successful" });

              } else {
                _that.$public.toast(res.msg);
              }
            });
        });
      }
    },


  },
  mounted () {
    wechat.wechatLogin(() => {

    });
  }
};
</script>

<style lang="stylus" scoped></style>